using System.Drawing.Drawing2D;

namespace SimulationPathViewer
{
    public partial class MainForm : Form
    {
        private List<(double X, double Y)> pathPoints = new List<(double X, double Y)>();
        private float scale = 1.0f;
        private PointF pan = new PointF(0, 0);
        private bool isPanning = false;
        private Point lastMousePos;

        public MainForm()
        {
            InitializeComponent();
            this.DoubleBuffered = true;

            // Add mouse wheel event for zooming
            this.MouseWheel += MainForm_MouseWheel;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            e.Graphics.Clear(Color.White);

            // Apply transformations
            e.Graphics.TranslateTransform(pan.X + ClientSize.Width / 2, pan.Y + ClientSize.Height / 2);
            e.Graphics.ScaleTransform(scale, scale);

            if (pathPoints.Count > 0)
            {
                using (Pen pen = new Pen(Color.Blue, 2))
                {
                    for (int i = 1; i < pathPoints.Count; i++)
                    {
                        e.Graphics.DrawLine(pen,
                            (float)pathPoints[i - 1].X, (float)pathPoints[i - 1].Y,
                            (float)pathPoints[i].X, (float)pathPoints[i].Y);
                    }
                }
            }
        }

        private void MainForm_MouseWheel(object? sender, MouseEventArgs e)
        {
            float oldScale = scale;
            if (e.Delta > 0)
                scale *= 1.1f;
            else
                scale /= 1.1f;

            scale = Math.Max(0.1f, Math.Min(10f, scale));
            Invalidate();
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if (e.Button == MouseButtons.Left)
            {
                isPanning = true;
                lastMousePos = e.Location;
                Cursor = Cursors.Hand;
            }
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (isPanning)
            {
                pan.X += e.X - lastMousePos.X;
                pan.Y += e.Y - lastMousePos.Y;
                lastMousePos = e.Location;
                Invalidate();
            }
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            if (e.Button == MouseButtons.Left)
            {
                isPanning = false;
                Cursor = Cursors.Default;
            }
        }

        public void LoadPathPoints(List<(double X, double Y)> points)
        {
            pathPoints = points;
            if (points.Count > 0)
            {
                // Auto-scale to fit the path
                double minX = points.Min(p => p.X);
                double maxX = points.Max(p => p.X);
                double minY = points.Min(p => p.Y);
                double maxY = points.Max(p => p.Y);

                double width = maxX - minX;
                double height = maxY - minY;

                // Calculate scale to fit the path in the window
                float scaleX = (float)(ClientSize.Width * 0.8 / width);
                float scaleY = (float)(ClientSize.Height * 0.8 / height);
                scale = Math.Min(scaleX, scaleY);

                // Center the path
                pan = new PointF(0, 0);
            }
            Invalidate();
        }
    }
}
